/**
 * Interface.java
 *
 * @author Philip Kwok
 * @author Myongsu Choe
 */


package SSF.OS.OSPF;


import java.util.*;
import SSF.Net.Util.IP_s;
import SSF.OS.BGP4.Util.AS_descriptor;
import SSF.OS.BGP4.Util.StringManip;


// ======================= class SSF.OS.OSPF.Interface ===================== //
/**
 * An OSPF interface.
 */
public class Interface {
  public int    typ;           // ptp, broadcast, or NBMA
  public int    ipaddr;
  public int    p2paddr;
  public byte   ipmask;
  public int    state;
  public String rtrnh;
  public String as_nh;
  public int    area_num;
  public String peer_as_nh;
  public int    peer_area_num;
  public String peer_nh;
  public int    cost;

  public Interface(int t, int ipa, int p2pa, byte mask, int st, String nh,
                   String asnh, int areano, String peer_asnh, int peer_areano,
                   String peernh, int c) {
    typ           = t;
    ipaddr        = ipa;
    p2paddr       = p2pa;
    ipmask        = mask;
    state         = st;
    rtrnh         = nh;
    as_nh         = asnh;
    area_num      = areano;
    peer_as_nh    = peer_asnh;
    peer_area_num = peer_areano;
    peer_nh       = peernh;
    cost          = c;
  }

  public String toString() {
    String str = "";
    str = "[Type: " + typ + ", IP_addr: " + IP_s.IPtoString(ipaddr) +
          ", PTP_addr: " + IP_s.IPtoString(p2paddr) + ", IP_mask: " +
          ipmask + ", State: " + state + ", ROUTER_NH: " + rtrnh +
          ", AS: " + as_nh + ", Area: " +
          area_num + ", Peer AS#: " + peer_as_nh + ", Peer AREAID: " +
          peer_area_num + " Peer ROUTER_NH: " + peer_nh + " Cost: " + cost;
    return str;
  }


  // ------------------------- toMinString(boolean) ------------------------ //
  /**
   * Converts the interface to a string using a briefer format than
   * <code>toString</code>.
   *
   * @param usenhi  Whether or not to use NHI addressing when possible.
   * @return the interface in minimal string format
   */
  public String toMinString(boolean usenhi) {
    String type = "";
    switch (typ) {
    case sOSPF.POINT_TO_POINT:  type = "p2p";  break;
    case sOSPF.TO_STUB:         type = "stb";  break;
    case sOSPF.TO_HOST:         type = "p2h";  break;
    }
    String str = type + " ";

    if (usenhi) {
      String nhiaddr = sOSPF.topnet.ip_to_nhi(IP_s.IPtoString(ipaddr));
      if (nhiaddr == null) {
        nhiaddr = "-";
      }
      str += StringManip.pad(nhiaddr, 8, ' ', true) + " ";

      nhiaddr = sOSPF.topnet.ip_to_nhi(IP_s.IPtoString(p2paddr));
      if (nhiaddr == null) {
        nhiaddr = "-";
      }
      str += StringManip.pad(nhiaddr, 8, ' ', true) + " ";
    } else {
      str += StringManip.pad(IP_s.IPtoString(ipaddr), 12, ' ', true) + " ";
      str += StringManip.pad(IP_s.IPtoString(p2paddr), 12, ' ', true) + " ";
    }

    str += ipmask +"   "+ state +"  "+ StringManip.pad(rtrnh,6,' ',true) + " ";
    
    if (usenhi) {
      String as_string = as_nh;
      if (as_nh.equals("")) {
        as_string = "<null>";
      }
      str += StringManip.pad(as_string,6,' ',true) + " ";
    } else {
      str += StringManip.pad(""+AS_descriptor.nh2as(as_nh),6,' ',true) + " ";
    }

    str += StringManip.pad(""+area_num,5,' ',true) + " ";

    if (usenhi) {
      str += StringManip.pad(peer_as_nh,6,' ',true) + " ";
    } else {
      str +=StringManip.pad(""+AS_descriptor.nh2as(peer_as_nh),6,' ',true)+" ";
    }

    str += StringManip.pad(""+peer_area_num,5,' ',true) + " " +
           StringManip.pad(""+peer_nh,6,' ',true) + " " + cost;

    return str;
  }

  // --------------------------- toMinString() --------------------------- //
  /**
   * Converts the interface to a string using a briefer format than
   * <code>toString</code>.
   *
   * @return the interface in minimal string format
   */
  public String toMinString() {
    return toMinString(false);
  }


} // end of class Interface
